package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.api.domain.*;
import cn.wolfcode.wolf2w.business.query.SearchQuery;
import cn.wolfcode.wolf2w.business.service.*;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 浪兎兎
 * @since 2025-06-12 15:20
 */
@RestController
@Slf4j
public class SearchController {

    @Autowired
    private IDestinationSearchService destinationSearchService;
    @Autowired
    private INoteSearchService noteSearchService;
    @Autowired
    private IQuestionSearchService questionSearchService;
    @Autowired
    private IStrategySearchService strategySearchService;
    @Autowired
    private IUserInfoSearchService userInfoSearchService;
    @Autowired
    private ISearchService searchService;

    @GetMapping("/search")
    public R<?> search(SearchQuery qo) {
        switch (qo.getType()) {
            case 0:
                return R.ok(searchDestination(qo));
            case 1:
                return R.ok(searchStrategy(qo));
            case 2:
                return R.ok(searchNote(qo));
            case 3:
                return R.ok(searchUserInfo(qo));
            case 4:
                return R.ok(searchQuestion(qo));
            default:
                return R.ok(searchAll(qo));
        }

    }

    private Page<Destination> searchDestination(SearchQuery qo) {
        return searchService.searchHighlight(Destination.class, DestinationSearch.class, qo, "title", "subTitle", "summary");
    }

    private Object searchAll(SearchQuery qo) {
        Page<Strategy> strategyPage = searchStrategy(qo);
        Page<Note> notePage = searchNote(qo);
        Page<UserInfo> userPage = searchUserInfo(qo);
        Page<Destination> destinationPage = searchDestination(qo);
        Page<Question> questionPage = searchQuestion(qo);
        long total = strategyPage.getTotalElements() + notePage.getTotalElements() + userPage.getTotalElements() + destinationPage.getTotalElements() + questionPage.getTotalElements();
        Map<String, Object> map = new HashMap<>();
        map.put("users", userPage.getContent());
        map.put("notes", notePage.getContent());
        map.put("strategies", strategyPage.getContent());
        map.put("dests", destinationPage.getContent());
        map.put("questions", questionPage.getContent());
        map.put("total", total);
        return map;
    }

    private Page<Question> searchQuestion(SearchQuery qo) {
        return searchService.searchHighlight(Question.class, QuestionSearch.class, qo, "title", "detailDoubt");
    }

    private Page<UserInfo> searchUserInfo(SearchQuery qo) {
        return searchService.searchHighlight(UserInfo.class, UserInfoSearch.class, qo, "nickname", "info", "city");
    }

    private Page<Note> searchNote(SearchQuery qo) {
        return searchService.searchHighlight(Note.class, NoteSearch.class, qo, "title", "summary");
    }

    private Page<Strategy> searchStrategy(SearchQuery qo) {
        return searchService.searchHighlight(Strategy.class, StrategySearch.class, qo, "title", "subTitle", "summary");
    }
}
